package com.care.controller.weixin;

import com.care.controller.vo.JsonResult;
import com.care.dao.mapper.*;
import com.care.dao.model.Information;
import com.care.utils.HtmlFilterUtil;
import com.care.utils.page.PageView;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import jakarta.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 前台：资讯API
 * @author xujinnan
 *
 */
@RequestMapping("/api/notice")
@RestController
public class InformationApiController extends ApiBaseController {

    @Autowired
    private InformationMapper informationMapper;

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期格式
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //注册自定义的编辑器
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 资讯列表
     * @param reqMap
     * @param request
     * @return
     */
    @RequestMapping("list")
    public JsonResult blogList(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) {
        PageView<Information> pageView = new PageView<>();
        pageView = pageView.startPage(reqMap);
        JsonResult result = new JsonResult(true, "0", "查询成功");
        Example example = new Example(Information.class);
        Criteria criteria = example.createCriteria();
        if(reqMap.containsKey("kw")&&StringUtils.isNotBlank(reqMap.get("kw").toString())){
            //查询帖子
            criteria.andLike("title", "%"+reqMap.get("kw").toString()+"%");
        }
        example.orderBy("publishTime").desc();
        List<Information> list = informationMapper.selectByExampleAndRowBounds(example, new RowBounds(pageView.getFirstResult(), pageView.getMaxresult()));
        for(Information info:list){
            info.setContent(HtmlFilterUtil.Html2Text(info.getContent()));
        }
        pageView.setRecords(list);
        pageView.setTotalrecord(informationMapper.selectCountByExample(example));
        result.setData(pageView);
        return result;
    }

    /**
     * 资讯详情
     * @param noticeId
     * @return
     */
    @RequestMapping("detail")
    public JsonResult forumDetail(Integer noticeId) {
        JsonResult result = new JsonResult(true, "0", "查询成功");
        Information information = informationMapper.selectByPrimaryKey(noticeId);
        if(StringUtils.isNotBlank(information.getHeadPic())){
            String[] url = information.getHeadPic().split(",");
            List<String> urls = new ArrayList<>();
            for (int i=0; i<url.length; i++) {
                urls.add(url[i]);
            }
            information.setPicList(urls);
        }
        result.setData(information);
        return result;
    }

}
